package com.itheima.reggie.controller;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;

@Controller
public class PayController {

    private final String APP_ID = "2021000121671683";
    private final String APP_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCkOJ9B97kkzVcpnDQPRepQcTdYUW9Y/XDQGXaYotf57PI0NMjMCHFTHsJdVD8WatgOzGgBORmEb5YNYVA2pEFkM1zXsvPMjbFdLV5XPW/uYwjKaj3CemuCMYhosQtDGpN8yKVMn71EEKloq/6p8mGCBoRO9veRRv/TGfqIoFx0plBIxhKCY5JFMOdHRdVR41MniAqkg6kiE0c/DfryWLP9Cc3OnDA+dhBv6bcewJXwf9hZVVfTVXbNvhMTzac1MbVKyntf/mRCdpLrtOFT0Qv0fXlPv+lbDYoxezEe9ARx73AgQRv/sdPsU6wayIKNicwD6ujh8DsWp+Hg/pwodVp9AgMBAAECggEASbKLbgxhDy4Dts294zjKHc2hO8ZYBQS/8vPqcDdsW2HFlD43+rCzQs0kBsQ/B61tRr7e0eWxGOkG2vxYBh2SpwgR208Y8CyKczlgBCCzMbVKBaTGEJfl1WirQehqGHQ9yhs1a3yocHpVvH6ra9M1hSetis50bcMCHdejwlrNAx/BPxmjfgWn8t58dSPlXP1A+PmoC5YxaiMPt+3AyguuNyhDA/TorvKjW32ZehaDNDEeiDRUCbDad6PihAi2nu4X5OjlLidvmKKvdQ5qog5XsZcR8p8AJlITwPRjMeVlssWWG8UKa5zRWiSfHZ26qwsk7SGqh1tnaklliC3GgkqJoQKBgQD0OgzszzEaDD+Bbvvh1NZAhb8X4+KAsCk5qiU8tYGcph+SORFl20vz160+DrwqDKXO/3HQvdsCi4A1As6oOniKMiugYiK0FOBIB2nJHvsh5oeSGD4Px2/Swjjd2xyCvBXpCv4AwCAsSka/p6oY5moyiwXW2+VWcrR8jczlWQ7pOQKBgQCsIz12uM0nFfT2tmrrGT43mMm7QI/qNzEAoT9laavstSbHIN8RakOsYPBLNh/dA0MvnLHnG9eSEQHrio6rxSSAbrCTkrHvqoQyFkPxQDiPuzqcE2/29872410QNsEUVOHVIXkNt1z3689xhJA8QyO+M6FPmxXqJyR7AspjkjEPZQKBgQClPDFKdtPNv8OnmqMoI4vhk5u6WiAA2S7LKOaQsGzEw1BQ6fNvanN0wtMfTXDZMj1sUz957zmh+yf+tauD/itXtKH3nO3F0On9UaaswB1Azmui2GwE2PY7LdSzpW8gtIhOZ3V6pIIf9RDw0ksvFZV8JLsKpzP+zRR3wKiD80E3YQKBgEdxWNEtYfgW5NSAYV+x2mC4yQEuioxq26unQOiWMwTLKoz615oiwHOKyDBvBkp0UC4ymqOQqqe051ca6pvf9oZXanHVMISIANXI5+n4/YCp9GlSp+WYdA2Dz9wq3XfSwufyPIu0zqEW/cHfgv1/4BXQ1+JyO2YzWLC1N8qbokEBAoGBALUSJnR1m5rZ1vYJOnAADNj3lr9E6KXwFSM7lpJTroWk34QkAY3MtQaNb2KmRZjMEnlZTbizhEp0166zBOkOYDRUEzZZ/hE3ZzOF8ZtkcXkWSR1dp206GRudSSpOCmvY9tnqVxg49hhnPuJaZ8AhyAWn6UyHixPqfDvfUMWMcaRS\n";
    private final String CHARSET = "UTF-8";
    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6UfTs0XonQlwdiCMXSwWBO72tLezj4tyepK+gV1s6vFe+Ai1jt08IpwpblbSiA/gkxcDZscHg+shZ088kp60cd77sedb+cGbMo3KdDWw8RZGPZnGDeRnv4+H/orgQRvEwBQojUj8aHcqwc0w5uogfTW/L4gbgNXwXx4WJp8ghSYP87s+tZtvwVYCFNwiud2RlUpJt9bTWkfXn0kk37DMxmtOKyyPmZlRsaI8A+Ubd+hAST4xghJk0MZVYRuEU5m0ZFD0/fOoYPrRk+a+wTTuM3Yk3KZdbGlMqw8sACj9ND477GMAhnaKWNOGA7OD4XdEUL1DJn5RaGDXI0vhXtN2ewIDAQAB\n";
    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL ="https://openapi.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = "http://localhost:8080/notifyUrl";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private final String RETURN_URL = "http://localhost:8080/returnUrl";


    @RequestMapping("alipay")
    public void alipay(HttpServletResponse httpResponse) throws IOException {

        Random r=new Random();
        //实例化客户端,填入所需参数
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //在公共参数中设置回跳和通知地址
        request.setReturnUrl(RETURN_URL);
        request.setNotifyUrl(NOTIFY_URL);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        //生成随机Id
        String out_trade_no = UUID.randomUUID().toString();
        //付款金额，必填
        String total_amount =Integer.toString(r.nextInt(9999999)+1000000);
        //订单名称，必填
        String subject ="奥迪A8 2016款 A8L 60 TFSl quattro豪华型";
        //商品描述，可空
        String body = "尊敬的会员欢迎购买奥迪A8 2016款 A8L 60 TFSl quattro豪华型";
        request.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String form = "";
        try {
            form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }
}




